1151A - Maxim and Biology - CodeForces Solution


brute force strings *1000

Please click on ads to support us..

Python Code:



n = int(input())
chaine = input()


lettres = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


def moves(a, b):
    
    i = lettres.index(a)
    j = lettres.index(b)
    return min(abs(i - j), 26 - abs(i - j))


mini = 1e10
for i in range(n - 3):
    som = 0
    for j, c in enumerate("ACTG"):
        som += moves(chaine[i + j], c)
    if som < mini:
        mini = som

print(mini)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define  ll long long int 
#define  all(x)  x.begin(),x.end()
#define  ll long long int  
int const mod = 1e9+7; 

ll dis (char c1, char c2 ) {
	  ll d1= 0, d2 = 0; 
	  string s; 
	  s+= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	  s+= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	  for (int i = c1-'A'; i<s.size(); i++) {
	  	 if(s[i]==c2)break; 
	  	 d1++; 
	  }
	  for (int i = c2-'A'; i<s.size(); i++) {
	  	 if(s[i]==c1)break; 
	  	 d2++;
	  }
	  return min(d1,d2);
}


void solve(){
	       
	       int n; cin >> n; string s; cin >> s; 
	       ll ans = 9999999999999999999999; 
	       for(int i = 0; i<=n-4; i++ ) {
	       	 ll tm = 0; 
	       	 tm += dis(s[i],'A');
	       	 tm += dis(s[i+1], 'C');
	       	 tm += dis(s[i+2], 'T');
	       	 tm += dis(s[i+3], 'G'); 
	       	 ans = min (tm, ans); 
	       }
	       cout << ans << endl; 
	       
	         
	         
	               
}
int main(){
	     
	      ios::sync_with_stdio(0);
	      cin.tie(0);cout.tie(0);
	
	      int t = 1; //cin >> t;
	      while(t--) 
	      solve(); 
	   
 return 0; 
}
 


Comments

Submit
0 Comments
More Questions

1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory